#include "interface.h"
#include "ui_interface.h"
#include "pack.h"
#include <QFile>

Interface::Interface(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Interface)
{
    ui->setupUi(this);
    client = new QTcpSocket(this);
    QFile qssFile(":/qss/styleinterface.qss");  // QSS文件路径
    ui->lineEdit_bpm->setReadOnly(true);
    ui->lineEdit_errmsg->setReadOnly(true);
    ui->lineEdit_tem_hum->setReadOnly(true);
    if (qssFile.open(QFile::ReadOnly | QFile::Text)) {
        QTextStream stream(&qssFile);
        this->setStyleSheet(stream.readAll());  // 设置给整个应用
        qssFile.close();
    }
    JdtMyPushButton *switchButton = new JdtMyPushButton(this);
    switchButton->setParent(this);
    switchButton->setSwitchSize(40, 20);
    switchButton->move(90,40);
    connect(switchButton,&JdtMyPushButton::clicked,this,[=](){
        if(switchButton->Right_Pos_Flag){
            Pack pack;
            pack.setType(TYPE_ORDER);
            QString order="open light";
            qDebug()<<"send success";
            pack<<order;
            pack>>client;
        }
        else if(switchButton->Left_Pos_Flag){
            Pack pack;
            pack.setType(TYPE_ORDER);
            QString order="close light";
            qDebug()<<"send success";
            pack<<order;
            pack>>client;
        }
    });
}

Interface::~Interface()
{
    delete ui;
}

void Interface::setClient(QTcpSocket *client)
{
    this->client = client;
}

void Interface::closeEvent(QCloseEvent *event)
{
    this->loginInterface->show();
    ui->lineEdit_bpm->clear();
    ui->lineEdit_errmsg->clear();
    ui->lineEdit_tem_hum->clear();
}

void Interface::setLoginInterface(QWidget *w)
{
    loginInterface = w;
}

void Interface::setLineEdit(QString str)
{
    ui->lineEdit_tem_hum->setText(str);
}

void Interface::setLineEdit_bpm(QString str)
{
    ui->lineEdit_bpm->setText(str);
}

void Interface::setLineEdit_errmsg(QString str)
{
    ui->lineEdit_errmsg->setText(str);
}

void Interface::setlabel_detected(int flag)
{
    // 保存原始样式以便恢复
    static QString originalStyle = ui->label_detected->styleSheet();
    static QString originalText = ui->label_detected->text();

    // 设置警告样式
    if(flag==1){
        ui->label_detected->setText("⚠️ 有物体靠近！");
        ui->label_detected->setStyleSheet("color: red; background-color: yellow; font-weight: bold; font-size: 14pt; border: 2px solid red; border-radius: 4px;");
        ui->label_detected->setVisible(true);
    }
    // 恢复原状
   if(flag==2){
        ui->label_detected->setText(originalText);
        ui->label_detected->setStyleSheet(originalStyle);
    };
}

void Interface::setlabel_lux(QString str)
{
    ui->label_lux->setText(str);
}

void Interface::on_pushButton_tem_hum_clicked()
{
    Pack pack;
    pack.setType(TYPE_ORDER);
    QString order="sht20";
    qDebug()<<"send success";
    pack<<order;
    pack>>client;
}


void Interface::on_pushButton_fanon_clicked()
{
    Pack pack;
    pack.setType(TYPE_ORDER);
    QString order="fanon";
    qDebug()<<"send success";
    pack<<order;
    pack>>client;
}


void Interface::on_pushButton_fanoff_clicked()
{
    Pack pack;
    pack.setType(TYPE_ORDER);
    QString order="fanoff";
    qDebug()<<"send success";
    pack<<order;
    pack>>client;
}


void Interface::on_pushButton_bpm_clicked()
{
    Pack pack;
    pack.setType(TYPE_ORDER);
    QString order="bpm";
    qDebug()<<"send success";
    pack<<order;
    pack>>client;
    this->setLineEdit_bpm("正在测量，请稍后......");
}

